<template>
  <div style="margin:30px 20px">
    <el-form :model="queryParams" ref="queryForm" :inline="true" >
      <el-form-item label="危化品名称" prop="materialName">
        <el-input v-model="queryParams.materialName" placeholder="请输入危化品名称" clearable size="small"
          @keyup.enter.native="getList" />
      </el-form-item>
      <el-form-item label="申购人员" prop="materialName">
        <el-input v-model="queryParams.applyTeacher" placeholder="请输入姓名或工号" clearable size="small"
          @keyup.enter.native="getList" />
      </el-form-item>
      <el-form-item label="类别" prop="hazardousOpt">
          <el-select v-model="queryParams.hazardousOpt" placeholder="请选择类别"  size="small">
            <el-option v-for="item in kindsOptions" :key="item.kindsId" :label="item.name" :value="item.kindsId">
          </el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="small" @click="getList">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="purchaseDetails">
      <el-table-column label="名称" prop="materialName" :show-overflow-tooltip="true" />
	  <el-table-column label="Cas号" prop="casNo" align="center" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="采购状态" align="center" prop="purchaseStatus" :formatter="purchaseStatusFormat" width="60"/>
      <el-table-column label="申购老师" prop="applyTeacher" align="center" :show-overflow-tooltip="true" width="80"/>
      <el-table-column label="类别" prop="hazardousOpt" align="center" :formatter="kindsFormat" width="80"/>
      <el-table-column label="单位" prop="unitOpt" align="center" :formatter="unitOptFormat" width="60"/>
      <el-table-column label="申购数量" prop="quantity" align="center" width="60"/>
      <el-table-column label="入库数量" prop="inventoryNum" align="center" width="60"/>
      <el-table-column label="包装重量" prop="packageWeight" align="center" width="60"/>
      <el-table-column label="实际重量" prop="realWeight" align="center" width="60"/>
      <el-table-column label="入库日期" prop="storageTime" align="center" width="100">
      </el-table-column>
      <el-table-column label="操作" align="center" width="60">
        <template slot-scope="scope">
          <el-button v-if="scope.row.purchaseStatus==1" size="mini" type="text" icon="el-icon-edit"
            @click="handleScanin(scope.row)" v-hasPermi="['labdanger:scanin:edit']">入库</el-button>
          <el-button v-if="scope.row.purchaseStatus==2" size="mini" type="text" icon="el-icon-edit"
            @click="getScanInfo(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改项目创建申请对话框 -->
    <el-dialog title="入库管理" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" v-if="!info" :rules="rules" label-width="100px">
        <el-form-item label="申购人员" class="item">
          {{form.applyTeacher}}
        </el-form-item>
        <el-row>
          <el-col :span="11">
            <el-form-item label="Cas号" class="item">
              {{form.casNo}}
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="名称" class="item">
              {{form.materialName}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="合同文档" class="item">
          上传附件为PDF或图片(jpg、png)
        </el-form-item>
        <el-form-item label="合做单位" prop="cooperation">
          <el-input v-model="form.cooperation" placeholder="请输入合作单位" type="string" size="small" class="input_cas" />
        </el-form-item>
        <el-form-item label="" class="item">
          <UploadCom ref="uploadContract" @submitData="submitContract" description="" :userFileList="contractFiles" />
        </el-form-item>
        <el-form-item label="发票附件" class="item">
          上传附件为PDF或图片(jpg、png)
        </el-form-item>
        <el-form-item label="发票编号" prop="invoiceNo">
          <el-input v-model="form.invoiceNo" placeholder="请输入发票编号" type="string" size="small" class="input_cas" />
        </el-form-item>
        <el-form-item label="" class="item">
          <UploadCom ref="uploadInvoice" @submitData="submitInovice" description="" :userFileList="inoviceFiles" />
        </el-form-item>
        <el-form-item label="报销凭证" class="item">
          上传附件为PDF或图片(jpg、png)
        </el-form-item>
        <el-form-item label="报销编码" prop="reimbursementCode">
          <el-input v-model="form.reimbursementCode" placeholder="请输入报销编码" type="string" size="small"
            class="input_cas" />
        </el-form-item>
        <el-form-item label="" class="item">
          <UploadCom ref="uploadReimbur" @submitData="submitData" description="" :userFileList="reimbursementFiles" />
        </el-form-item>
        <el-form-item label="包装重量" prop="packageWeight">
          <el-input v-model="form.packageWeight" placeholder="请输入包装重量" type="string" size="small" class="input_cas" />
        </el-form-item>
        <el-form-item label="试剂重量" prop="realWeight">
          <el-input v-model="form.realWeight" placeholder="请输入试剂重量" type="number" size="small" class="input_cas" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="入库信息" :visible.sync="info" width="700px" append-to-body>
      <el-form ref="form" :model="form" v-if="info" label-width="100px">
        <el-form-item label="申购人员" class="item">
          {{form.applyTeacher}}
        </el-form-item>
        <el-row>
          <el-col :span="11">
            <el-form-item label="名称" class="item">
              {{form.materialName}}
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="Cas号" class="item">
              {{form.casNo}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="合做单位" class="item">
          {{form.cooperation}}
        </el-form-item>
        <el-form-item label="合同附件" class="item">
          <UploadCom ref="uploadContract" disabled class="uploadCss" :userFileList="form.contractFiles" />
        </el-form-item>
        <el-form-item label="发票附件" class="item">
          上传附件为PDF或图片(jpg、png)
        </el-form-item>
        <el-form-item label="发票编号" class="item">
          {{form.invoiceNo}}
        </el-form-item>
        <el-form-item label="发票附件" class="item">
          <UploadCom ref="uploadInvoice" disabled class="uploadCss" :userFileList="form.inoviceFiles" />
        </el-form-item>
        <el-form-item label="报销凭证" class="item">
          上传附件为PDF或图片(jpg、png)
        </el-form-item>
        <el-form-item label="报销编码" class="item">
          {{form.reimbursementCode}}
        </el-form-item>
        <el-form-item label="报销附件" class="item">
          <UploadCom ref="uploadReimbur" disabled class="uploadCss" :userFileList="form.reimbursementFiles" />
        </el-form-item>
        <el-form-item label="包装重量" prop="packageWeight" class="item">
          {{form.packageWeight}}
        </el-form-item>
        <el-form-item label="试剂重量" prop="realWeight" class="item">
          {{form.realWeight}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancelInfo">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listScandata,
    scanin,
    getScanInfo
  } from "@/api/labdanger/purchaseDet.js";
  import {
    getCasTypeByNo
  } from "@/api/labdata/casType";
  import {
    getAllRestrictedKinds
  } from "@/api/labdata/cylindersType";
  import UploadCom from "@/views/labdata/commons/upload.vue";
  export default {
    name: "QueryScaninData",
    components: {
      UploadCom
    },
    data() {
      return {
        //合同文档
        contractFiles: [],
        //发票文档
        inoviceFiles: [],
        //报销文档
        reimbursementFiles: [],
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 在用化学品表格数据
        purchaseDetails: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        info: false,
        open: false,
        // 类别字典
        kindsOptions: [],
        // 单位字典
        unitOptOptions: [],
        // 采购状态字典
        purchaseStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          cooperation: [{
            required: true,
            message: "合作单位不能为空",
            trigger: "blur"
          }],
          invoiceNo: [{
            required: true,
            message: "发票编号不能为空",
            trigger: "blur"
          }],
          packageWeight: [{
            required: true,
            message: "包装重量不能为空",
            trigger: "blur"
          }],
          realWeight: [{
            required: true,
            message: "试剂重量不能为空",
            trigger: "change"
          }],

        }
      };
    },
    created() {
      this.getAllKinds();
      this.getList();
      this.getDicts("unitOpt").then(response => {
        this.unitOptOptions = response.data;
      });
      this.getDicts("purchaseStatusOpt").then(response => {
        this.purchaseStatusOptions = response.data;
      });

    },
    methods: {
      /** 查询在用化学品列表 */
      getList() {
        this.loading = true;
        listScandata(this.queryParams).then(response => {
          this.purchaseDetails = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams =
        { pageNum: 1,
          pageSize: 10
        };
        this.getList();
      },
      getAllKinds() {
        getAllRestrictedKinds().then(response => {
          this.kindsOptions = response.data;
        });
      },
      // 类别字典翻译
      kindsFormat(row, column) {
        for (let i = 0; i < this.kindsOptions.length; i++) {
          if (this.kindsOptions[i].kindsId == row.hazardousOpt) {
            return this.kindsOptions[i].name;
          }
        }
        return '';
      },
      // 单位字典翻译
      unitOptFormat(row, column) {
        return this.selectDictLabel(this.unitOptOptions, row.unitOpt);
      },
      // 采购状态字典翻译
      purchaseStatusFormat(row, column) {
        return this.selectDictLabel(this.purchaseStatusOptions, row.purchaseStatus);
      },
      /** 入库按钮操作 */
      handleScanin(row) {
        this.open = true;
        this.contractFiles = [];
        this.inoviceFiles = [];
        this.reimbursementFiles = [];
        this.form = row;
      },
      /** 查看按钮操作 */
      getScanInfo(row) {
        this.info = true;
        getScanInfo(row.purchaseDetailId).then(response => {
          this.form = response.data;
        });
      },
      cancel() {
        this.open = false;
        this.contractFiles = [];
        this.inoviceFiles = [];
        this.reimbursementFiles = [];
      },
      cancelInfo() {
        this.open = false;
        this.info = false;
      },
      submitData(files) {
        this.form.reimbursementFiles = files;
        scanin(this.form).then(response => {
          this.msgSuccess("入库成功");
          this.open = false;
          this.getList();
        });
      },
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$refs["uploadContract"].doSubmit();
          }
        });
      },
      submitContract(files) {
        this.form.contractFiles = files;
        this.$refs["uploadInvoice"].doSubmit();
      },
      submitInovice(files) {
        this.form.inoviceFiles = files;
        this.$refs["uploadReimbur"].doSubmit();
      }

    }
  };
</script>
<style type="text/css" scoped>
  .input_cas {
    width: 300px;
  }
  .item {
    margin-bottom: 0px;
  }
  .uploadCss .el-upload {
    display: none !important;
  }
</style>
